<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Attendance_cycle;
use App\Models\Student;
use App\Models\Student_attendance;
use App\Models\Squad;
use App\Models\Crouse_time;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class Attendance_viewController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student_attendance::class, function (Grid $grid) {
            $squads = Squad::where('class_teacher','=',Admin::user()->id)->pluck('id')->toArray();

            $grid->model()->wherein('squad_id', $squads);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            })->sortable();
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            })->sortable();
            $grid->attendance_cycle_id(trans('admin.attendance_cycle_id'))->display(function($attendance_cycle_id){
                return Attendance_cycle::find($this->attendance_cycle_id)->name;
            })->sortable();
            $grid->attendance_date(trans('admin.attendance_date'))->sortable();
            
            $grid->crouse_time_id(trans('admin.crouse_time_id'))->display(function($crouse_time_id){
                return Crouse_time::find($this->crouse_time_id)->name;
            })-> sortable();
            $grid->classroom_id(trans('admin.classroom'))->display(function($classroom_id){
                return Classroom::find($this->classroom_id)->name;
            })->sortable();
            $grid->attendance_status(trans('admin.status'))->display(function ($attendance_status) {
                if($this->attendance_status==1){
                    return "<span class='label bg-green'>正常</span>";
                }elseif ($this->attendance_status==2) {
                    return "<span class='label bg-blue'>请假</span>";
                }elseif ($this->attendance_status==3) {
                    return "<span class='label bg-orange'>迟到</span>";
                }elseif ($this->attendance_status==4) {
                    return "<span class='label bg-red'>缺勤</span>";
                }
            })->sortable();

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableDelete();
            });

            $grid->disableCreateButton();
            //$grid->disableFilter();
            //$grid->disableActions();
            //禁用导出按钮
            //$grid->disableExport();
            $filename="考勤记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('squad_id', trans('admin.squad'))->select(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name', 'id'));
                    $filter->equal('attendance_cycle_id', trans('admin.attendance_cycle_id'))->select(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'));
                    $filter->between('attendance_date', trans('admin.attendance_date'))->date();
                    $filter->equal('classroom_id', trans('admin.classroom'))->select(Classroom::all()->pluck('name', 'id'));
                });
                $filter->column(1/2, function ($filter) {
                    $filter->where(function ($query) {
                        $query->whereHas('student', function ($query) {
                            $query->where('en_name', 'like', "%{$this->input}%");
                        });

                    }, '英文名');
                    $filter->equal('crouse_time_id',trans('admin.crouse_time_id'))->select(Crouse_time::all()->pluck('name', 'id'));
                    $filter->equal('attendance_status', trans('admin.attendance_status'))->select([
                        '1'     => '正常',
                        '2'     => '请假',
                        '3'     => '迟到',
                        '4'     => '缺勤'
                    ]);
                });
            });
        });
    }

    protected function form()
    { 
        $form = new Form(new Student_attendance());

        $form->display('id', 'ID');

        $form->select('attendance_cycle_id', '考勤周期')->options(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->date('attendance_date',trans('admin.attendance_date'))->format('YYYY-MM-DD')->addElementLabelClass('must-input');
        $form->select('crouse_time_id',trans('admin.crouse_time_id'))->options(Crouse_time::all()->pluck('name','id'))->addElementLabelClass('must-input');
        
        $form->select('classroom_id', trans('admin.classroom'))->options(Classroom::all()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->select('attendance_status',trans('admin.attendance_status'))->options([
                '1' => '正常',
                '2' => '请假',
                '3' => '迟到',
                '4' => '缺勤'
            ])->rules('required')->addElementLabelClass('must-input');
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        // $form->saved(function (Form $form) {
        //     $student_id = $form->model()->student_id;
        //     // 跳转页面
        //     return redirect('/admin/attendance_entering/'.$student_id);
        //     });
        return $form;
    }
}
